package org.farec.tcc.spring.controller;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.farec.tcc.dao.LocalDAO;
import org.farec.tcc.dao.PermissaoDAO;
import org.farec.tcc.modelo.Local;
import org.farec.tcc.modelo.Permissao;
import org.farec.tcc.modelo.Usuario;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LocalController {
	
	@RequestMapping("/formLocal")
	public String formLocal(Model model, HttpServletRequest request)
	{	
		Connection connection = (Connection) request.getAttribute("conexao");
		
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario,"item");
		
		for(Permissao p : permissaoList)
		{
			if(p.getModulo().equals("Local") && p.getItem().equals("adicionar"))
			{
				return "cadlocal";
			}
		}
		
		return "index";
		
	}
	
	@RequestMapping("/adicionarLocal")
	public String adiciona(Local local, Model model, HttpServletRequest request) throws SQLException
	{
		Connection connection = (Connection) request.getAttribute("conexao");
		
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario,"item");
		
		for(Permissao p : permissaoList)
		{
			if(p.getModulo().equals("Local") && p.getItem().equals("adicionar"))
			{
				LocalDAO dao = new LocalDAO(connection);
				Map<String, Object> params = new LinkedHashMap<String, Object>();
				if(dao.getLocal(local).isAlerta() == true) //Se existir local no banco ou seja, true != true
				{
					params.put("local", true);
					return "cadlocal";
				}
				dao.adiciona(local);
				return "redirect:listaLocal";
			}
		}
		
		return "index";
	}
	
	@RequestMapping("/listaLocal")
	public String lista(Model model, HttpServletRequest request)
	{
		Connection connection = (Connection) request.getAttribute("conexao");
		
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario,"item");
		List<Permissao> permissaoList2 = new ArrayList<>();
		
		for(Permissao p : permissaoList)
		{
			permissaoList2.add(daoPermissao.getPermissao("Local", p));
			
		}
		
		if(permissaoList2.size() > 0 )
		{
			LocalDAO dao = new LocalDAO(connection);
			
			model.addAttribute("permissoesList", permissaoList2);
			model.addAttribute("locais", dao.getLista());
			return "local";
		}
			
		return "index";
	}
	
	@RequestMapping("/exibeLocal")
	public String mostra(int id, Model model, HttpServletRequest request)
	{
		Connection connection = (Connection) request.getAttribute("conexao");
		
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario,"item");
		
		for(Permissao p : permissaoList)
		{
			if(p.getModulo().equals("Local") && p.getItem().equals("alterar"))
			{
				LocalDAO dao = new LocalDAO(connection);
				model.addAttribute("locais", dao.buscaPorCodigo(id));
				return "editlocal";
			}
		}
		
		return "index";
	}
	
	@RequestMapping("/alterarLocal")
	public String altera(Local local, Model model, HttpServletRequest request) throws SQLException
	{
		Connection connection = (Connection) request.getAttribute("conexao");
		
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario,"item");
		
		for(Permissao p : permissaoList)
		{
			if(p.getModulo().equals("Local") && p.getItem().equals("alterar"))
			{
				LocalDAO dao = new LocalDAO(connection);
				Map<String, Object> params = new LinkedHashMap<String, Object>(); 
				if(dao.getLocalUp(local).isAlerta() == true) //Se existir area no banco ou seja, true != true
				{
					model.addAttribute("locais", dao.buscaPorCodigo(local.getCodigo()));
					params.put("local", true);
					return "editlocal";
				}
				
				dao.altera(local);
				return "redirect:listaLocal";
			}
		}
		
		return "index";
		
	}
		
	@RequestMapping("/removeLocal")
	public String remove(Local local, HttpServletRequest request)
	{
		Connection connection = (Connection) request.getAttribute("conexao");
		
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario,"item");
		
		for(Permissao p : permissaoList)
		{
			if(p.getModulo().equals("Local") && p.getItem().equals("remover"))
			{	
				LocalDAO dao = new LocalDAO(connection);
				dao.remove(local);
				return "redirect:listaLocal";
			}
		}
		
		return "index";
	}

}
